package com.helehousekeeping.xlrequest;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.helehousekeeping.xlrequest.cmd.BaseCmdHttpTask;
import com.helehousekeeping.xlrequest.cmd.CmdCheckphone;
import com.helehousekeeping.xlrequest.cmd.CmdUtil;
import com.helehousekeeping.xlrequest.utils.MyUtils;

import org.json.JSONException;
import org.json.JSONObject;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private ProgressDialog mProgressDialog;
    private TextView       mTv_phone;
    private Button         mButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initListener();

    }

    private void initListener() {
        mTv_phone.setOnClickListener(this);
        mButton.setOnClickListener(this);
    }

    private void initView() {
        mTv_phone = (TextView) findViewById(R.id.tv_phone);
        mButton = (Button) findViewById(R.id.button);
    }

    @Override
    public void onClick(View view) {
        int Id = view.getId();
        switch (Id) {
            case R.id.button:
                checkPhone();
                break;
            default:
                break;
        }

    }

    private void checkPhone() {

        final String phone = mTv_phone.getText().toString();
        if (checkInfo(phone)) {
            showProgresDialog();
            // Toast.makeText(MainActivity.this, "001", Toast.LENGTH_SHORT).show();
            String params = CmdCheckphone.CreateParams(phone);
            CmdUtil.sendNoEncryptRequest("http://dzb.ebenny.com/app.php?c=Register&act=Reg", params, new BaseCmdHttpTask.CmdListener() {
                @Override
                public void onCmdExecuted(String responseResult) {
                    handleResult(responseResult, phone);
                    closeProgressDialog();
                }

                @Override
                public void onCmdException(Throwable exception) {
                    Toast.makeText(MainActivity.this, "网络异常", Toast.LENGTH_SHORT).show();
                    closeProgressDialog();

                }
            });

        }
    }

    private void handleResult(String responseResult, String phone) {
        CmdCheckphone.Results results = CmdUtil.convertJson2Object(responseResult, CmdCheckphone.Results.class);
        if (results == null) {
            JSONObject jsonObject;
            try {
                jsonObject = new JSONObject(responseResult);
                String msg = jsonObject.optString("MESSAGE");
                Toast.makeText(MainActivity.this, "提交失败，" + msg, Toast.LENGTH_SHORT).show();
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return;
        }
        String status = results.STAUTS;
        String msg = results.MESSAGE;
        Log.i("返回信息：", results.toString());
        if (status.equals("1")) {
            Toast.makeText(MainActivity.this, "已注册", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(MainActivity.this, "未注册", Toast.LENGTH_SHORT).show();
        }
    }

    //登录信息检查
    private boolean checkInfo(String phone) {
        if (TextUtils.isEmpty(phone)) {
            Toast.makeText(MainActivity.this, "手机号码不能为空", Toast.LENGTH_SHORT).show();
            return false;
        }
        if (!MyUtils.isPhone(phone)) {
            MyUtils.showToast("请输入正确的手机号码");
            return false;
        }
        return true;
    }

    private void showProgresDialog() {
        if (mProgressDialog == null) {
            mProgressDialog = new ProgressDialog(this, ProgressDialog.THEME_HOLO_LIGHT);
            mProgressDialog.setMessage("正在提交信息，请耐心等待......");
            mProgressDialog.setCanceledOnTouchOutside(false);
            mProgressDialog.setCancelable(false);
            mProgressDialog.show();
        } else if (!mProgressDialog.isShowing()) {
            mProgressDialog.show();
        }
    }

    private void closeProgressDialog() {
        if (mProgressDialog.isShowing() && mProgressDialog != null) {
            mProgressDialog.dismiss();
        }
    }


}
